import Vue from 'vue'
import 'normalize.css/normalize.css' // A modern alternative to CSS resets
import 'babel-polyfill' // 解决vue在ie的兼容性问题
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import '@/styles/index.scss' // global css

import App from './App'
import router from './router'
import store from './store'
import axios from 'axios'

import {
  publicFun
} from "cnf-publicfun"

import {
  dingTalkPc
} from 'dingtalk-auth'

import '@/icons' // icon

Vue.use(ElementUI)
Vue.prototype.$http = axios
Vue.config.productionTip = false



// 得到corpId 参数
localStorage.setItem('corpId', decodeURIComponent((new RegExp('[?|&]' + 'corpId' + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ''])[1].replace(/\+/g, '%20')) || '');
localStorage.setItem('appId', decodeURIComponent((new RegExp('[?|&]' + 'appId' + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ''])[1].replace(/\+/g, '%20')) || '');



// localStorage.setItem('corpId', 'ding07fbc4578cc7f57e35c2f4657eb6378f'); //为了测试
// localStorage.setItem('appId', '17088'); //为了测试
// // console.log(localStorage.getItem('corpId'), 'corpId');
// // console.log(localStorage.getItem('appId'), 'appId');

const way = '2'; //1之前 2ddlwp

if (way == '1') {

  // let jsListApi = ['biz.contact.choose', 'biz.util.uploadImage', 'biz.ding.post', 'biz.contact.departmentsPicker']
  // dingTalkPc.ddConfig(jsListApi, '/api/isv/auth/getJsConfig').then(confingRes => {
  DingTalkPC.runtime.permission.requestAuthCode({
    corpId: localStorage.getItem('corpId'),
    onSuccess: function (result) {
      let authUrl = `/api/isv/auth/login?corpId=${localStorage.getItem('corpId')}&code=${result.code}&appId=${localStorage.getItem('appId')}`;
      axios.get(authUrl).then(function (res) {
        if (res.data.success) {
          localStorage.setItem('userid', res.data.result.user.userid)
          localStorage.setItem('userAvatar', res.data.result.user.avatar)
          localStorage.setItem('userName', res.data.result.user.name)

          axios.defaults.headers.common['Authorization'] = res.data.result.token // 请求头文件

          new Vue({
            el: '#app',
            router,
            store,
            render: h => h(App)
          })
        } else {
          ElementUI.Message.warning({
            message: res.data.errorMsg || `免登失败`,
            type: 'warning'
          })
        }


      }, err => {
        ElementUI.Message.warning({
          message: err || `免登失败`,
          type: 'warning'
        })
      })
    },
    onFail: err => {
      ElementUI.Message.warning({
        message: `获取临时授权码失败`,
        type: 'warning'
      })
    }
  })
  // }, err => {
  //   ElementUI.Message.warning({
  //     message: err.errorMessage || `签名失败`,
  //     type: 'warning'
  //   })
  // })
} else {
  // 改成lwp方式不要签名，有此方式免登
  ddlwp(localStorage.getItem("corpId")).then(res => {
    console.log('ddlwp', res);
    if (res.success) {
      queryRbacNewVue();
    } else {
      ElementUI.Message.warning({
        message: res.errorMsg || '用户信息获取失败',
        type: "warning"
      });
    }
  }, err => {
    ElementUI.Message.warning({
      message: err,
      type: "warning"
    });
  });

  async function queryRbacNewVue() {
    // let res = await queryRbac();
    new Vue({
      el: '#app',
      router,
      store,
      render: h => h(App)
    })
  }

  function queryRbac() {
    return new Promise((resolve) => {
      axios.get('/api/isv/rbac/queryRbac').then(res => {
        if (res.data.success) {
          axios.defaults.headers.common['Authorization'] = res.data.result.token; //请求头文件
          sessionStorage.setItem("myPower", res.data.result.qx || "");
        }
        resolve("ok");
      }, err => {
        resolve("ok");
      });
    });
  }
}


// new Vue({
//   el: '#app',
//   router,
//   store,
//   render: h => h(App)
// })
